package cn.cqs.redisitem.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @discription :自定义安全信息
 * @author: cqs
 * @date: 2020-09-11 10:25
 */


public class CustomRealm extends AuthorizingRealm {
    /**
     * 功能描述 :支持Jwttoken
     *
     * @param token:
     * @return boolean
     * @author cqs
     * @date 2020/9/11
     */

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 功能描述 :角色 授权
     *
     * @param principals:
     * @return org.aache.shiro.authz.AuthorizationInfo
     * @author cqs
     * @date 2020/9/11
     */

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取用户名, 用户唯一标识
        String username = (String) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        Set<String> permissionSet = shiroService.listPermissions(username);
//        permissionSet.add("perm-1");    // 造数据, 假装是从数据库查出来的
//        permissionSet.add("perm-2");
//        simpleAuthorizationInfo.setStringPermissions(permissionSet);
//        Set<String> roleSet = shiroService.listRoles(username);
//        roleSet.add("role-1");    // 造数据, 假装是从数据库查出来的
//        roleSet.add("role-2");
//        simpleAuthorizationInfo.setRoles(roleSet);
        return simpleAuthorizationInfo;

    }

    /**
     * 功能描述 :认证
     *
     * @param authenticationToken:
     * @return org.apache.shiro.authc.AuthenticationInfo
     * @author cqs
     * @date 2020/9/11
     */

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String token=  authenticationToken.getPrincipal().toString();
//        token解密（获取用户信息）
//        判断token是否过期
//        判断用户是否锁定


        return null;
    }
}
